package com.zyw.common;

import java.io.Serializable;

public class RespResult<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	protected Integer code;
	protected String message;
	protected T data;
	protected Long timestamp;

	public RespResult() {
		super();
		this.timestamp = System.currentTimeMillis();
	}
	
	public RespResult(RespCode respCode) {
		this.code = respCode.getId();
		this.message = respCode.getDesc();
		this.timestamp = System.currentTimeMillis();
	}
	
	public RespResult(Integer code, T data) {
		super();
		this.code = code;
		this.data = data;
		this.timestamp = System.currentTimeMillis();
	}
	
	public boolean hasErrors() {
		return !RespCode.OK.getId().equals(this.code);
	}
	
	public RespResult<T> addError(String message) {
		this.message = message;
		this.code = RespCode.ERROR.getId();
		this.timestamp = System.currentTimeMillis();
		return this;
	}
	
	public RespResult<T> addError(RespCode respCode) {
		this.code = respCode.getId();
		this.message = respCode.getDesc();
		this.timestamp = System.currentTimeMillis();
		return this;
	}
	
	public RespResult<T> addError(Integer code, String message) {
		this.code = code;
		this.message = message;
		this.timestamp = System.currentTimeMillis();
		return this;
	}
	
	public RespResult<T> addSuccess(T data) {
		this.code = RespCode.OK.getId();
		this.message = RespCode.OK.getDesc();
		this.data = data;
		this.timestamp = System.currentTimeMillis();
		return this;
	}

	
	// getter, setter ...
	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Long getTimestamp() {
		return timestamp;
	}

}
